﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AttTypeDefine;

public class TurnTable : MonoBehaviour
{
    
    #region 成员变量
    public float turnspeed = 70f;
    public static EPianoGameType m_oNowGame;//当前游戏界面
    EPianoGameType m_oNextGame;//下一个游戏界面
    Vector3 pos;          //手当前点坐标
    Vector3 Beginpos;     //按下时坐标
    Vector3 Lastpos;      //上一帧坐标
    Vector3 Firstrotation;//刚按下时转盘的角度
    float Nowrotation;    //z轴的变化量
    Vector3 basepoint;    //圆盘中心点    
    Vector3 Begin;        //起始向量
    int change = 0;  
    float m_fRadius;
    bool m_bAllkey = false, b = true;
    Camera cam;
    private void Awake()
    {
        transform.rotation = Quaternion.identity;
        cam = GlobalHelper.UICam;
        m_fRadius = gameObject.GetComponent<RectTransform>().rect.width/2/100;
        m_oNowGame = EPianoGameType.eGame1;
        basepoint = transform.position/100;//UGUI坐标->世界坐标
    }
    #endregion

    void Update()
    {
        if (GlobalHelper.IsTurn)
        {
#if UNITY_EDITOR
            //编辑器模式实时检测鼠标按下情况
            MouseController();
#else
        //手机端模式实时检测手指按下情况
        TouchController();
#endif
        }
    }

    #region 编辑器模式实时检测鼠标按下情况
    void MouseController()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Firstrotation = transform.rotation.eulerAngles; //开始触摸点位置
            pos = new Vector3(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y, 0);   
            Beginpos = pos;
            float dis = Vector3.Distance(pos, basepoint);
            if (dis <= m_fRadius * 0.87 && dis>= m_fRadius * 0.25)
            {
                m_bAllkey = true;
            }
            //开始触摸点的位置与转盘原点之间的向量
            Begin = pos - transform.position;
            Lastpos = Beginpos;
        }
        else if (Input.GetMouseButton(0) && m_bAllkey == true)
        {
            Removed();
        }
        else if (Input.GetMouseButtonUp(0) && m_bAllkey == true)
        {
            FixPostion();
            SwitchUI();
            b = true;
            m_bAllkey = false;
        }
    }
    #endregion

    #region  手机端模式实时检测手指按下情况
    //认为最多不会超过10个
    bool[] m_bCanJoy = new bool[10];
    void TouchController()
    {
        for (int i = 0; i < Input.touchCount; i++)
        {
            Touch touch = Input.touches[i];
            switch (touch.phase)
            {
                case TouchPhase.Began:
                    {
                        m_bCanJoy[i] = false;
                        Firstrotation = transform.rotation.eulerAngles;
                        pos = new Vector3(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y, 0);
                        Beginpos = pos;
                        float dis = Vector3.Distance(pos, basepoint);
                        if (dis <= m_fRadius * 0.87 && dis >= m_fRadius * 0.25)
                        {
                            m_bCanJoy[i] = true;

                        }
                        //开始触摸点的位置与转盘原点之间的向量
                        Begin = pos - transform.position;
                        Lastpos = Beginpos;
                        break;
                    }
                case TouchPhase.Moved:
                    {
                        if (!m_bCanJoy[i]) return;
                        Removed();
                        break;
                    }
                case TouchPhase.Ended:
                    {
                        if (!m_bCanJoy[i]) return;
                        FixPostion();
                        SwitchUI();
                        b = true;
                        break;
                    }
                case TouchPhase.Canceled:
                    {
                        if (!m_bCanJoy[i]) return;
                        break;
                    }
                case TouchPhase.Stationary:
                    {
                        if (!m_bCanJoy[i]) return;
                        break;
                    }
            }
        }
    }
    #endregion

    #region 移动
    private void Removed()
    {
        //触摸点的位置
        pos = new Vector3(cam.ScreenToWorldPoint(Input.mousePosition).x, cam.ScreenToWorldPoint(Input.mousePosition).y, 0);
        #region 拖动旋转
        if (Beginpos.x > pos.x && pos != Lastpos)
        {
            transform.Rotate(new Vector3(0, 0, -1), turnspeed * (Lastpos.x - pos.x), Space.Self);       //绕自身Z轴旋转向右转，速度为1
        }
        if (Beginpos.x < pos.x && pos != Lastpos)
        {
            transform.Rotate(new Vector3(0, 0, 1), turnspeed * (pos.x - Lastpos.x), Space.Self);      //绕自身Z轴旋转向左转，速度为1
        }
        #endregion
        Lastpos = pos;
        Nowrotation = transform.rotation.eulerAngles.z;
        #region 根据现在转盘的角度值决定松开后转盘的角度值
        if (Nowrotation < 45 || Nowrotation > 315)
        {
            change = 0;
        }
        else if (Nowrotation > 45 && Nowrotation < 135)
        {
            change = 1;
        }
        else if (Nowrotation > 135 && Nowrotation < 225)
        {
            change = 2;
        }
        else if (Nowrotation > 225 && Nowrotation < 315)
        {
            change = 3;
        }
        #endregion
    }
    #endregion

    #region 定位
    void FixPostion()
    {
        #region 点击定位
        float angle = Vector3.Angle(new Vector3(0, -1, 0), Begin);
        if (angle > 45 && Beginpos.x > 0 && transform.rotation.eulerAngles.z == Firstrotation.z)
        {
            transform.rotation *= Quaternion.Euler(0, 0, -90);
            b = false;
        }
        if (angle > 45 && Beginpos.x < 0 && transform.rotation.eulerAngles.z == Firstrotation.z)
        {
            transform.rotation *= Quaternion.Euler(0, 0, 90);
            b = false;
        }
        #endregion
        if (b == true)
        {
            #region 旋转定位
            switch (change)
            {
                case 0:
                    {
                        transform.rotation = Quaternion.Euler(0, 0, 0);
                        break;
                    }
                case 1:
                    {
                        transform.rotation = Quaternion.Euler(0, 0, 90);
                        break;
                    }
                case 2:
                    {
                        transform.rotation = Quaternion.Euler(0, 0, 180);
                        break;
                    }
                case 3:
                    {
                        transform.rotation = Quaternion.Euler(0, 0, 270);
                        break;
                    }
            }
            #endregion
        }
    }
    #endregion

    #region 判断用哪个场景UI
    void SwitchUI()
    {
        if (gameObject.transform.rotation.eulerAngles.z < 1 || gameObject.transform.rotation.eulerAngles.z > 359)
        {
            m_oNextGame = EPianoGameType.eGame1;
        }
        else if (gameObject.transform.rotation.eulerAngles.z < 271 && gameObject.transform.rotation.eulerAngles.z > 269)
        {
            m_oNextGame = EPianoGameType.eGame2;
        }
        else if (gameObject.transform.rotation.eulerAngles.z < 181 && gameObject.transform.rotation.eulerAngles.z > 179)
        {
            m_oNextGame = EPianoGameType.eGame3;
        }
        else if (gameObject.transform.rotation.eulerAngles.z < 91 && gameObject.transform.rotation.eulerAngles.z > 89)
        {
            m_oNextGame = EPianoGameType.eGame4;
        }
        else
        {
            Debug.LogErrorFormat("error");
        }
        if (m_oNextGame != m_oNowGame)
        {
            AudioMgr.PlayeAudio(gameObject, AttTypeDefine.EAudioType.Audio_Switch, "Switch");
            m_oNowGame = m_oNextGame;
        }
    }
    #endregion
}
